package na.learn.ejb2;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Hashtable;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import na.learn.ejb2.bom.AbstractConsumer;

public class ConsumerFacadeBean implements SessionBean, ConsumerFacadeInterface {
	SessionContext ctx = null;

	ConsumerDetailsCMPLocalHome consumerDetailsHome = null;

	ConsumerDetailsCMPLocal consumerDetailsLocal = null;

	Collection consumers = null;

	private static final String EJB_JNDI_NAME = "java:comp/env/ConsumerDetailsLocal";

	public void ejbCreate() throws CreateException {
		initContext();
	}

	public void createConsumer(AbstractConsumer aConsumer) {
		Integer returnId = null;

		if (consumerDetailsLocal == null) {
			initContext();
		}

		try {
			consumerDetailsLocal = (ConsumerDetailsCMPLocal) (consumerDetailsHome.create(aConsumer.getId(),
					aConsumer.getName(), aConsumer.getAge(), aConsumer.getContactNumber()));

			returnId = consumerDetailsLocal.getConsumerId();
		} catch (ClassCastException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CreateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Collection searchConsumer(AbstractConsumer consumer) {

		try {
			if (consumer.getId() != null)
				consumers.add((ConsumerDetailsCMPLocal) PortableRemoteObject.narrow(consumerDetailsHome
						.findByPrimaryKey(consumer.getId()), ConsumerDetailsCMPLocal.class));
			else if (consumer.getAge() != null)
				consumers.add((ConsumerDetailsCMPLocal) PortableRemoteObject.narrow(consumerDetailsHome
						.findByConsumerAge(consumer.getAge()), ConsumerDetailsCMPLocal.class));
			else if (consumer.getName() != null)
				consumers.add((ConsumerDetailsCMPLocal) PortableRemoteObject.narrow(consumerDetailsHome
						.findByConsumerName(consumer.getName()), ConsumerDetailsCMPLocal.class));

		} catch (ClassCastException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FinderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return consumers;

	}

	private void initContext() {
		try {
			Hashtable parameters = new Hashtable();

			parameters.put(Context.PROVIDER_URL, "t3://localhost:7001");
			parameters.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
			parameters.put(Context.SECURITY_PRINCIPAL, "admin");
			parameters.put(Context.SECURITY_CREDENTIALS, "sumodemo");

			InitialContext ctx = new InitialContext(parameters);

			Object objRef = ctx.lookup(EJB_JNDI_NAME);
			consumerDetailsHome = (ConsumerDetailsCMPLocalHome) objRef;

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassCastException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
		ctx = arg0;
	}

	public void ejbRemove() throws EJBException, RemoteException {
	}

	public void ejbActivate() throws EJBException, RemoteException {
	}

	public void ejbPassivate() throws EJBException, RemoteException {
	}

}
